package com.xiaocg.factory;

import com.xiaocg.factory.dd.CoffeeFactory;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ConfigFileFactory {

    private static Map<String, Coffee> map = new HashMap<>();

    static {
        InputStream resourceAsStream = ConfigFileFactory.class.getClassLoader().getResourceAsStream("com/xiaocg/factory/bean.properties");
        Properties p = new Properties();
        try {
            p.load(resourceAsStream);
            for (Object key:p.keySet()) {
                String property = p.getProperty((String) key);
                Class aClass = Class.forName(property);
                Coffee o = (Coffee)aClass.getDeclaredConstructor().newInstance();
                map.put((String)key,o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Coffee createCoffee(String name){
        return map.get(name);
    }
}
